﻿using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 碰撞到，把事件传递给父节点
    /// </summary>
    public class PhysicsParentDelivery : MonoBehaviour
    {
        //public GameObject go;
        [Header("参数")]
        public string args;

        public IPhysicsCollisionEnter colEnter;
        public IPhysicsCollisionStay colStay;
        public IPhysicsCollisionExit colExit;
        public IPhysicsTriggerEnter tEnter;
        public IPhysicsTriggerStay tStay;
        public IPhysicsTriggerExit tExit;

        public void OnCollisionEnter(Collision other)
        {
            if (colEnter == null)
            {
                colEnter = gameObject.GetComponentInParent<IPhysicsCollisionEnter>();
            }

            if (colEnter != null)
            {
                colEnter.OnCollisionEnter(other, this);
            }
        }

        public void OnCollisionStay(Collision other)
        {
            if (colStay == null)
            {
                colStay = gameObject.GetComponentInParent<IPhysicsCollisionStay>();
            }

            if (colStay != null)
            {
                colStay.OnCollisionStay(other, this);
            }
        }

        public void OnCollisionExit(Collision other)
        {
            if (colExit == null)
            {
                colExit = gameObject.GetComponentInParent<IPhysicsCollisionExit>();
            }

            if (colExit != null)
            {
                colExit.OnCollisionExit(other, this);
            }
        }

        public void OnTriggerEnter(Collider other)
        {
            if (tEnter == null)
            {
                tEnter = gameObject.GetComponentInParent<IPhysicsTriggerEnter>();
            }

            if (tEnter != null)
            {
                tEnter.OnTriggerEnter(other, this);
            }
        }

        public void OnTriggerStay(Collider other)
        {
            if (tStay == null)
            {
                tStay = gameObject.GetComponentInParent<IPhysicsTriggerStay>();
            }

            if (tStay != null)
            {
                tStay.OnTriggerStay(other, this);
            }
        }

        public void OnTriggerExit(Collider other)
        {
            if (tExit == null)
            {
                tExit = gameObject.GetComponentInParent<IPhysicsTriggerExit>();
            }

            if (tExit != null)
            {
                tExit.OnTriggerExit(other, this);
            }
        }
    }
}